import UIAbility from '@ohos.app.ability.UIAbility';
import hilog from '@ohos.hilog';
import window from '@ohos.window';
import Want from '@ohos.app.ability.Want';

export const pay_number1: string = 'pay_number'

export const TAG: string = '生命周期'

/**
 * 新建Ability：在同级目录下，右键-new-Ability。
 * // 同步会在module.json5 (权限配置) 中形成相应配置
 * 注意这也是默认使用index页面。会和之前Ability抢页面，所以一般需要另建一个page，并修改新Ability中的 windowStage.loadContent('pages/Index'
 */
export default class PayAbility extends UIAbility {
  onCreate(want, launchParam) {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
    // (want as Want).parameters[pay_number]
    // (want as Want).parameters['pay_number']
    let num: number = (want as Want).parameters[pay_number1] as number
    hilog.info(0x0000, TAG, 'Ability onCreate hilog 取值为 ', num);
    console.info(TAG, 'Ability onCreate 取值为 ', num);
    // 05-19 17:41:16.648 32100-19299/com.walker.hmfirst I 00000/生命周期: Ability onCreate hilog 取值为
    // 05-19 17:41:16.648 32100-19299/com.walker.hmfirst I 0FEFE/JsApp: 生命周期 Ability onCreate 取值为  0.01
    // 发现hilog很多不输出，所以一般建议是console

    // ④获取值后。使用AppStorage与页面共享
    AppStorage.SetOrCreate(pay_number1, num)
  }

  onDestroy() {
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
  }

  onWindowStageCreate(windowStage: window.WindowStage) {
    // Main window is created, set main page for this ability
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');

    windowStage.loadContent('pages/pay/PayPage', (err, data) => {
      if (err.code) {
        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
        return;
      }
      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
    });
  }

  onWindowStageDestroy() {
    // Main window is destroyed, release UI related resources
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
  }

  onForeground() {
    // Ability has brought to foreground
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
  }

  onBackground() {
    // Ability has back to background
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
  }
}
